﻿Imports lablinkhelper

Public Class InsertUser
    Inherits System.Windows.Forms.Form
    Private mv_iStatus As Status
    Private mv_bSuccess As Boolean = False
    Private mv_sUserName As String = "UnKnown"
    Public mv_bAdmin As Boolean = False

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub
    Public Sub New(ByVal pv_sTitle As String)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.Text = pv_sTitle
        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Protected Friend WithEvents cmdSave As System.Windows.Forms.Button
    Protected Friend WithEvents txtDepart As System.Windows.Forms.TextBox
    Protected Friend WithEvents txtDesc As System.Windows.Forms.TextBox
    Protected Friend WithEvents txtFullName As System.Windows.Forms.TextBox
    Protected Friend WithEvents txtUserName As System.Windows.Forms.TextBox
    Protected Friend WithEvents lblMoTa As System.Windows.Forms.Label
    Protected Friend WithEvents lblDonVi As System.Windows.Forms.Label
    Protected Friend WithEvents lblTenNguoiDung As System.Windows.Forms.Label
    Protected Friend WithEvents lblTenDangNhap As System.Windows.Forms.Label
    Friend WithEvents cmdClose As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(InsertUser))
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.txtDepart = New System.Windows.Forms.TextBox
        Me.txtDesc = New System.Windows.Forms.TextBox
        Me.txtFullName = New System.Windows.Forms.TextBox
        Me.txtUserName = New System.Windows.Forms.TextBox
        Me.lblMoTa = New System.Windows.Forms.Label
        Me.lblDonVi = New System.Windows.Forms.Label
        Me.lblTenNguoiDung = New System.Windows.Forms.Label
        Me.lblTenDangNhap = New System.Windows.Forms.Label
        Me.cmdSave = New System.Windows.Forms.Button
        Me.cmdClose = New System.Windows.Forms.Button
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.txtDepart)
        Me.GroupBox1.Controls.Add(Me.txtDesc)
        Me.GroupBox1.Controls.Add(Me.txtFullName)
        Me.GroupBox1.Controls.Add(Me.txtUserName)
        Me.GroupBox1.Controls.Add(Me.lblMoTa)
        Me.GroupBox1.Controls.Add(Me.lblDonVi)
        Me.GroupBox1.Controls.Add(Me.lblTenNguoiDung)
        Me.GroupBox1.Controls.Add(Me.lblTenDangNhap)
        Me.GroupBox1.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.GroupBox1.Location = New System.Drawing.Point(0, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(344, 172)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Thông tin người dùng"
        '
        'txtDepart
        '
        Me.txtDepart.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtDepart.Location = New System.Drawing.Point(112, 68)
        Me.txtDepart.MaxLength = 100
        Me.txtDepart.Name = "txtDepart"
        Me.txtDepart.Size = New System.Drawing.Size(216, 22)
        Me.txtDepart.TabIndex = 2
        Me.txtDepart.Text = ""
        '
        'txtDesc
        '
        Me.txtDesc.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtDesc.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtDesc.Location = New System.Drawing.Point(112, 92)
        Me.txtDesc.MaxLength = 255
        Me.txtDesc.Multiline = True
        Me.txtDesc.Name = "txtDesc"
        Me.txtDesc.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtDesc.Size = New System.Drawing.Size(216, 64)
        Me.txtDesc.TabIndex = 3
        Me.txtDesc.Text = ""
        '
        'txtFullName
        '
        Me.txtFullName.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtFullName.Location = New System.Drawing.Point(112, 44)
        Me.txtFullName.MaxLength = 100
        Me.txtFullName.Name = "txtFullName"
        Me.txtFullName.Size = New System.Drawing.Size(216, 22)
        Me.txtFullName.TabIndex = 1
        Me.txtFullName.Text = ""
        '
        'txtUserName
        '
        Me.txtUserName.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper
        Me.txtUserName.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtUserName.Location = New System.Drawing.Point(112, 20)
        Me.txtUserName.MaxLength = 50
        Me.txtUserName.Name = "txtUserName"
        Me.txtUserName.Size = New System.Drawing.Size(216, 22)
        Me.txtUserName.TabIndex = 0
        Me.txtUserName.Text = ""
        '
        'lblMoTa
        '
        Me.lblMoTa.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblMoTa.Location = New System.Drawing.Point(4, 96)
        Me.lblMoTa.Name = "lblMoTa"
        Me.lblMoTa.Size = New System.Drawing.Size(104, 16)
        Me.lblMoTa.TabIndex = 26
        Me.lblMoTa.Text = "Mô tả thêm"
        Me.lblMoTa.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'lblDonVi
        '
        Me.lblDonVi.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblDonVi.Location = New System.Drawing.Point(4, 72)
        Me.lblDonVi.Name = "lblDonVi"
        Me.lblDonVi.Size = New System.Drawing.Size(104, 16)
        Me.lblDonVi.TabIndex = 24
        Me.lblDonVi.Text = "Phòng ban"
        Me.lblDonVi.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'lblTenNguoiDung
        '
        Me.lblTenNguoiDung.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblTenNguoiDung.Location = New System.Drawing.Point(4, 48)
        Me.lblTenNguoiDung.Name = "lblTenNguoiDung"
        Me.lblTenNguoiDung.Size = New System.Drawing.Size(104, 16)
        Me.lblTenNguoiDung.TabIndex = 22
        Me.lblTenNguoiDung.Text = "Tên đầy đủ"
        Me.lblTenNguoiDung.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'lblTenDangNhap
        '
        Me.lblTenDangNhap.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblTenDangNhap.Location = New System.Drawing.Point(4, 24)
        Me.lblTenDangNhap.Name = "lblTenDangNhap"
        Me.lblTenDangNhap.Size = New System.Drawing.Size(104, 16)
        Me.lblTenDangNhap.TabIndex = 20
        Me.lblTenDangNhap.Text = "Tên người dùng"
        Me.lblTenDangNhap.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'cmdSave
        '
        Me.cmdSave.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.cmdSave.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdSave.Image = CType(resources.GetObject("cmdSave.Image"), System.Drawing.Image)
        Me.cmdSave.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.cmdSave.Location = New System.Drawing.Point(164, 180)
        Me.cmdSave.Name = "cmdSave"
        Me.cmdSave.Size = New System.Drawing.Size(80, 25)
        Me.cmdSave.TabIndex = 4
        Me.cmdSave.Text = "&Ghi"
        '
        'cmdClose
        '
        Me.cmdClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdClose.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.cmdClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.cmdClose.Image = CType(resources.GetObject("cmdClose.Image"), System.Drawing.Image)
        Me.cmdClose.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.cmdClose.Location = New System.Drawing.Point(260, 180)
        Me.cmdClose.Name = "cmdClose"
        Me.cmdClose.Size = New System.Drawing.Size(80, 25)
        Me.cmdClose.TabIndex = 17
        Me.cmdClose.Text = "Th&oát"
        '
        'InsertUser
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(346, 215)
        Me.Controls.Add(Me.cmdClose)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.cmdSave)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "InsertUser"
        Me.ShowInTaskbar = False
        Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Thêm người dùng"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Properties"
    Public Property ps_UserName() As String
        Get
            Return mv_sUserName
        End Get
        Set(ByVal Value As String)
            mv_sUserName = Value
        End Set
    End Property
    Public Property ps_iStatus() As Status
        Get
            Return mv_iStatus
        End Get
        Set(ByVal Value As Status)
            mv_iStatus = Value
        End Set
    End Property
    Public Property pb_Success() As Boolean
        Get
            Return mv_bSuccess
        End Get
        Set(ByVal Value As Boolean)
            mv_bSuccess = Value
        End Set
    End Property
#End Region

    Private Sub InsertFunction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Select Case mv_iStatus
                Case globalModule.Status.Update 'Cập nhật
                    GetDataForUpdate()
                Case Else
            End Select
        Catch ex As Exception

        End Try
    End Sub

    Private Sub _KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Try
            If Asc(e.KeyChar) = Keys.Enter Then e.Handled = True
        Catch ex As Exception
        End Try
    End Sub
    Private Sub GetDataForUpdate()
        Dim sv_oclsUser As New clsUser
        Dim sv_DS As New DataSet
        Try
            sv_DS = sv_oclsUser.dsGetUserInfor(mv_sUserName)
            If sv_DS.Tables(0).Rows.Count > 0 Then
                txtUserName.Enabled = False
                txtUserName.Text = sv_DS.Tables(0).Rows(0)("PK_sUID")
                txtFullName.Text = sv_DS.Tables(0).Rows(0)("sFullName")
                txtDepart.Text = sv_DS.Tables(0).Rows(0)("sDepart")
                txtDesc.Text = sv_DS.Tables(0).Rows(0)("sDesc")
            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Function mf_bCheckValidData() As Boolean
        Try
            If txtUserName.Text.Trim.Equals(String.Empty) Then
                MessageBox.Show("Bạn phải nhập UserName(tên truy cập)", gv_sAnnouncement, MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtUserName.Focus()
                Return False
            End If
            Return True
        Catch ex As Exception
            MessageBox.Show("Lỗi cần gặp người lập trình" & ex.Message, gv_sAnnouncement, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return False
        End Try
    End Function
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim sv_oclsUser As New clsUser
        Dim sv_sUserName As String = ""
        Dim sv_sFullName As String = ""
        Dim sv_sDepart As String = ""
        Dim sv_sDesc As String = ""
        Dim sv_oEncrypt As New Encrypt(gv_sSymmetricAlgorithmName)
        Try
            If mf_bCheckValidData() Then
                sv_sUserName = ValidData(txtUserName.Text)
                sv_sFullName = ValidData(txtFullName.Text)
                sv_sDepart = ValidData(txtDepart.Text)
                sv_sDesc = ValidData(txtDesc.Text)
                mv_sUserName = sv_sUserName
                Select Case mv_iStatus
                    Case globalModule.Status.Update
                        If sv_oclsUser.UpdateUser(gv_sBranchID, sv_sUserName, sv_sFullName, sv_sDepart, sv_sDesc) Then
                            mv_bSuccess = True
                            Dim sv_oDR As DataRow
                            For Each sv_oDR In gv_dsUser.Tables(0).Rows
                                If sv_oDR.Item("PK_sUID").ToString.Trim.ToUpper.Equals(sv_sUserName.ToUpper) Then
                                    With sv_oDR
                                        .Item("sFullName") = sv_sFullName
                                        .Item("sDepart") = sv_sDepart
                                        .Item("sDesc") = sv_sDesc
                                    End With
                                    Exit For
                                End If
                            Next
                            gv_dsUser.Tables(0).AcceptChanges()
                            Me.Close()
                        Else
                            MessageBox.Show("Lỗi cập nhật người dùng", gv_sAnnouncement, MessageBoxButtons.OK, MessageBoxIcon.Information)
                            mv_bSuccess = False
                            Return
                        End If
                    Case globalModule.Status.Insert
                        If sv_oclsUser.InsertUser(gv_sBranchID, sv_sUserName, sv_sFullName, sv_oEncrypt.Mahoa(""), 0, sv_sDepart, sv_sDesc, mv_bAdmin) Then
                            mv_bSuccess = True
                            If Not mv_bAdmin Then
                                Dim sv_oDR As DataRow
                                sv_oDR = gv_dsUser.Tables(0).NewRow
                                With sv_oDR
                                    .Item("spwd") = sv_oEncrypt.Mahoa("")
                                    .Item("iSecurityLevel") = 0
                                    .Item("PK_sUID") = sv_sUserName
                                    .Item("sFullName") = sv_sFullName
                                    .Item("sDepart") = sv_sDepart
                                    .Item("sDesc") = sv_sDesc
                                End With
                                gv_dsUser.Tables(0).Rows.Add(sv_oDR)
                                gv_dsUser.Tables(0).AcceptChanges()
                            End If
                            If mv_bAdmin Then
                                Me.Close()
                            Else
                                If mv_bSuccess Then
                                    Dim sv_oNode As New TreeNode(sv_sUserName)
                                    With sv_oNode
                                        .Tag = "LEAFUSER#"
                                        .SelectedImageIndex = ImageIndex.NodeUser
                                        .ImageIndex = ImageIndex.NodeUser
                                        .ForeColor = Color.Navy
                                        .NodeFont = New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
                                    End With

                                    'Thêm vào DataSet chức năng này
                                    gv_oMainForm.tvwAdminSystem.SelectedNode.Nodes.Add(sv_oNode)
                                    gv_oMainForm.tvwAdminSystem.SelectedNode.ExpandAll()
                                End If
                                If gv_bCloseFormAfterDML Then
                                    Me.Close()
                                Else
                                    txtUserName.Text = ""
                                    txtFullName.Text = ""
                                    txtDepart.Text = ""
                                    txtDesc.Text = ""
                                    txtUserName.Focus()
                                End If
                            End If
                        Else
                            MessageBox.Show("Đã tồn tại " & IIf(mv_bAdmin, " quản trị", " người dùng") & " có UserName như vậy", gv_sAnnouncement, MessageBoxButtons.OK, MessageBoxIcon.Information)
                            txtUserName.Focus()
                            mv_bSuccess = False
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gv_sAnnouncement, MessageBoxButtons.OK, MessageBoxIcon.Information)
            mv_bSuccess = False
            Return
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub InsertUser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    ProcessTabKey(True)
                Case Keys.Escape
                    Me.Close()
            End Select
        Catch ex As Exception

        End Try
    End Sub
End Class
